package top.tangyh.lamp.care.mapper;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BPgRecord;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.query.PgRecordInVO;
import top.tangyh.lamp.care.vo.result.IsbarPgRecordDTO;
import top.tangyh.lamp.care.vo.result.PgRecordOutVO;
import top.tangyh.lamp.care.vo.result.StatisticsByMonth;
import top.tangyh.lamp.care.vo.save.PgRecordVo;
import top.tangyh.lamp.common.utils.PDateUtils;
import top.tangyh.lamp.common.utils.YDateUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * Mapper 接口
 * 评估评分
 * </p>
 *
 * @author zqb
 * @date 2024-07-09 08:35:22
 * @create [2024-07-09 08:35:22] [zqb] [代码生成器生成]
 */
@Repository
public interface BPgRecordMapper extends SuperMapper<BPgRecord> {

    default boolean checkPg(String patientIdent, LocalDateTime gradeTime, Long createBy, String getPgName){
        //获取系统默认时区
        ZoneId zoneId = ZoneId.systemDefault();
//时区的日期和时间
        ZonedDateTime zonedDateTime = gradeTime.atZone(zoneId);
//获取时刻
        Date dateOfGrade = Date.from(zonedDateTime.toInstant());

        Date start = PDateUtils.stringToDate(PDateUtils.format(dateOfGrade,PDateUtils.DATE_TIME_MINUTE_PATTERN)+":00",PDateUtils.DATE_TIME_PATTERN);
        Date end = PDateUtils.stringToDate(PDateUtils.format(dateOfGrade,PDateUtils.DATE_TIME_MINUTE_PATTERN)+":59",PDateUtils.DATE_TIME_PATTERN);
        LambdaQueryWrapper<BPgRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPgRecord::getPatientIdent,patientIdent)
                .eq(BPgRecord::getPgName,getPgName)
                .eq(BPgRecord::getCreatedBy,createBy)
                .ge(BPgRecord::getGradeTime,start)
                .le(BPgRecord::getGradeTime,end);
        List<BPgRecord> olds = selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(olds)){
            return true;
        }
        else{
            return false;
        }
    }

    default List<BPgRecord> selectByIds(List<Long> ids){
        LambdaQueryWrapper<BPgRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BPgRecord::getId,ids);
        return selectList(queryWrapper);
    }

    List<PgRecordOutVO> queryOnryRecord(PgRecordInVO param) ;

    List<BPgRecord> getPgRecodOfTengTong(String hospitalNumber,String startTime,String endTime,String pgCode);


    default List<BPgRecord> selectByTimeAndUserAndPgid(String patientIdent, Long time, Set<Long> pgid,String operator){
        Date start = YDateUtils.stringToDate(YDateUtils.format(new Date(time),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":00",YDateUtils.DATE_TIME_PATTERN);
        Date end = YDateUtils.stringToDate(YDateUtils.format(new Date(time),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":59",YDateUtils.DATE_TIME_PATTERN);
        LambdaQueryWrapper<BPgRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPgRecord::getPatientIdent,patientIdent)
                .ge(BPgRecord::getGradeTime,start).eq(BPgRecord::getCreatedBy,operator)
                .le(BPgRecord::getGradeTime,end).in(BPgRecord::getPgId,pgid);
        return selectList(queryWrapper);
    }

    default void deleteByTimeAndUserAndPgid(String patientIdent, Long time, Set<Long> pgid,String operator){
        Date start = YDateUtils.stringToDate(YDateUtils.format(new Date(time),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":00",YDateUtils.DATE_TIME_PATTERN);
        Date end = YDateUtils.stringToDate(YDateUtils.format(new Date(time),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":59",YDateUtils.DATE_TIME_PATTERN);
        LambdaQueryWrapper<BPgRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPgRecord::getPatientIdent,patientIdent)
                .ge(BPgRecord::getGradeTime,start).eq(BPgRecord::getCreatedBy,operator)
                .le(BPgRecord::getGradeTime,end).in(BPgRecord::getPgId,pgid);
        delete(queryWrapper);
    }

    default  List<BPgRecord> selectLatestByParams(String patientIdent, String hospitalNumber, Long pgid, Long recordTime,Long rkTime){
        LambdaQueryWrapper<BPgRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BPgRecord::getPatientIdent,patientIdent).eq(BPgRecord::getHospitalNumber,hospitalNumber)
                .le(BPgRecord::getGradeTime,new Date(recordTime)).eq(BPgRecord::getPgId,pgid);
        if(null != rkTime){
            queryWrapper.ge(BPgRecord::getGradeTime,new Date(rkTime));
        }
        queryWrapper.orderByDesc(BPgRecord::getGradeTime).last(" limit 1");
        return selectList(queryWrapper);
    }

    List<PgRecordVo>  getDiseaseOverviewPgRecord(String hospitalNumber, Date rkTime, Date ckTime);


    /**
     *  获取最近一条评分--用于isbar交班
     *
     */
    List<IsbarPgRecordDTO> getLatestPgRecordAll(@Param("pgCode") String pgCode, @Param("hospitalNumbers") Collection<String> hospitalNumbers, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    /**
     *  获取最近一条评分--用于isbar交班
     *
     */
    List<IsbarPgRecordDTO> getLatestPgRecord(String pgCode, String hospitalNumber, Date startTime, Date endTime);



    List<StatisticsByMonth> painStatisticsOfSixMonth(String startTime,String pgName);

    @Select("SELECT pg.*, pt.patient_name " +
            "FROM b_pg_record pg " +
            "LEFT JOIN b_patient pt ON pg.patient_ident = pt.patient_ident " +
            "WHERE pg.pg_name = #{pgName} " +
            "AND (pg.grade_time >= #{startTime} OR #{startTime} IS NULL) " +
            "AND (pg.grade_time <= #{endTime} OR #{endTime} IS NULL) " +
            "ORDER BY pg.created_time DESC")
    IPage<BPgRecord> selectPgRecordWithPatientName(Page<?> page,
                                                   @Param("pgName") String pgName,
                                                   @Param("startTime") String startTime,
                                                   @Param("endTime") String endTime);
}


